package com.yuanchu.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.common.utils.DateFormatUtils;
import com.yuanchu.common.utils.PageResult;
import com.yuanchu.store.dao.YoshopRegionDao;
import com.yuanchu.store.dao.YoshopStoreAddressDao;
import com.yuanchu.store.dto.AddressDto;
import com.yuanchu.store.dto.EditAddressDto;
import com.yuanchu.store.dto.QueryPageParams;
import com.yuanchu.store.entity.YoshopStoreAddressEntity;
import com.yuanchu.store.service.YoshopStoreAddressService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("yoshopStoreAddressService")
public class YoshopStoreAddressServiceImpl extends ServiceImpl<YoshopStoreAddressDao, YoshopStoreAddressEntity> implements YoshopStoreAddressService {


    @Resource
    private YoshopRegionDao regionDao;


    @Override
    public PageResult<YoshopStoreAddressEntity> queryStoreAddress(QueryPageParams params, Integer storeId) {
        if (params == null){
            params = new QueryPageParams();
        }
        LambdaQueryWrapper<YoshopStoreAddressEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(YoshopStoreAddressEntity::getStoreId, storeId);
        lqw.eq(params.getType() != null, YoshopStoreAddressEntity::getType, params.getType());
        lqw.like(StringUtils.isNotEmpty(params.getSearch()), YoshopStoreAddressEntity::getName, params.getSearch());
        Page<YoshopStoreAddressEntity> page = new Page<>(params.getPage(), params.getPagesize());
        Page<YoshopStoreAddressEntity> pageResult = this.page(page, lqw);
        List<YoshopStoreAddressEntity> list = pageResult.getRecords();
        list.forEach(item -> item.setCreateTime(DateFormatUtils.formatDatetime(item.getCreateTime())));
        return PageResult.formatPageResult(list, params.getPage(), params.getPagesize(), pageResult.getTotal());
    }

    @Transactional
    @Override
    public boolean addStoreAddress(EditAddressDto form, Integer storeId) {
        AddressDto addressDto = form.getForm();
        YoshopStoreAddressEntity address =  new YoshopStoreAddressEntity();
        List<Integer> cascader = addressDto.getCascader();
        address.setProvinceId(cascader.get(0));
        address.setPhone(addressDto.getPhone());
        address.setCityId(cascader.get(1));
        address.setRegionId(cascader.get(2));
        address.setDetail(addressDto.getDetail());
        address.setName(addressDto.getName());
        addressDto.setPhone(addressDto.getPhone());
        address.setSort(addressDto.getSort());
        address.setType(addressDto.getType());
        address.setStoreId(storeId);
        address.setCreateTime(String.valueOf(Instant.now().toEpochMilli()/1000));
        address.setUpdateTime(String.valueOf(Instant.now().toEpochMilli()/1000));
        return this.save(address);
    }
    @Transactional
    @Override
    public boolean editStoreAddress(EditAddressDto form, Integer storeId) {
        AddressDto addressDto = form.getForm();
        YoshopStoreAddressEntity address =  new YoshopStoreAddressEntity();
        address.setAddressId(Integer.valueOf(form.getAddressId()));
        List<Integer> cascader = addressDto.getCascader();
        address.setProvinceId(cascader.get(0));
        address.setCityId(cascader.get(1));
        address.setRegionId(cascader.get(2));
        address.setDetail(addressDto.getDetail());
        address.setName(addressDto.getName());
        addressDto.setPhone(addressDto.getPhone());
        address.setType(addressDto.getType());
        address.setSort(addressDto.getSort());
        address.setStoreId(storeId);
        address.setUpdateTime(String.valueOf(Instant.now().toEpochMilli()/1000));
        return this.updateById(address);
    }

    @Override
    public boolean deleteStoreAddress(EditAddressDto form, Integer storeId) {
        return this.removeById(Integer.valueOf(form.getAddressId()));
    }

    @Override
    public List<YoshopStoreAddressEntity> queryStoreAddressAll(Integer type, Integer storeId) {
        LambdaQueryWrapper<YoshopStoreAddressEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(YoshopStoreAddressEntity::getIsDelete, 0);
        lqw.eq(YoshopStoreAddressEntity::getStoreId, storeId);
        lqw.eq(YoshopStoreAddressEntity::getType, type);
        List<YoshopStoreAddressEntity> list = this.list(lqw);
        list.forEach(item -> {
            item.setCreateTime(DateFormatUtils.formatDatetime(item.getCreateTime()));
            Map<String, Object> region = regionDao.getAddressByRegionId(item.getRegionId());
            item.setRegion(region);
            List<Integer> cascader = new ArrayList<>();
            cascader.add(item.getProvinceId());
            cascader.add(item.getCityId());
            cascader.add(item.getRegionId());
            item.setCascader(cascader);
            String fullAddress = region.get("province").toString() + region.get("city").toString() + region.get("region").toString() + item.getDetail();
            item.setFullAddress(fullAddress);
        });
        return list;

    }
}